package yxk.utils;

import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class ConutFieldsUtil {

    public void beforeDoOperation(String billNo) {
/**
 * 1.caltasks 获取任务集合  查询calrulev 获取计算规则版本
 *  再获取calrulev.totalsalary.id 总薪资  calrulev.netsalary.id 净薪资 为了获取薪酬项目id
 */
        //获取   核算任务集合
        QFilter qFilter = new QFilter("billno", QCP.equals,billNo);
        DynamicObject caltasks = BusinessDataServiceHelper.loadSingle("hsas_approvebill", "id,caltasks.number as caltasks,totalpersonnum,calentryentity.calpersonid", qFilter.toArray());
        List<Long> caltasksNumber =new ArrayList<>();
        String caltaskId = "";
        int totalpersonnum = 0;
        if(caltasks == null){
            return;
        }
        DynamicObjectCollection collection = caltasks.getDynamicObjectCollection("caltasks");
        for (DynamicObject row : collection) {
            DynamicObject based = row.getDynamicObject("fbasedataid");
            caltasksNumber.add((Long)based.getPkValue());
        }
        caltaskId = caltasks.getPkValue().toString();
        totalpersonnum = caltasks.getInt("totalpersonnum");
        //根据获取的 核算任务ID 获取计算规则版本和薪酬项目id（总薪资，净薪资）
        DataSet calpayrolltaskRows = QueryServiceHelper.queryDataSet(this.getClass().getName(), "hsas_calpayrolltask",
                "calrulev,calrulev.totalsalary.number as totalsalary,calrulev.netsalary.number as netsalary", new QFilter("id", QCP.in,caltasksNumber).toArray(), null);
        if(calpayrolltaskRows.isEmpty()){
            return;
        }
        String totalsalary = "";
        String netsalary = "";
        for (Row row:calpayrolltaskRows){
            totalsalary = row.getString("totalsalary");
            netsalary = row.getString("netsalary");
        }
        /**
         * 2.按照任务把核算列表进行分批 根据对应的核算名单，核算名单 hsas_calperson  caltask
         */
        //获取核算名单
        DataSet calpersonsRow = QueryServiceHelper.queryDataSet(this.getClass().getName(), "hsas_approvebill", "hsas_approvebill.calentryentity.calpersonid as calpersonid", qFilter.toArray(), null);
        ArrayList<Long> calpersonsList = new ArrayList<Long>();

        for (Row row:calpersonsRow){
            calpersonsList.add(row.getLong("calpersonid"));
        }
        if(calpersonsList.isEmpty()){
            return;
        }

        BigDecimal calamountValue = null;
        BigDecimal netsalaryValue = null;
        //总薪资合计
        qFilter = new QFilter("calpersonid", QCP.in,calpersonsList);
        qFilter.and(new QFilter("hsas_caltableentry.salaryitem.number",QCP.equals,totalsalary));
        DataSet caltableRows = QueryServiceHelper.queryDataSet(this.getClass().getName(), "hsas_caltable", "hsas_caltableentry.calamountvalue as calamountvalue", qFilter.toArray(), null).groupBy().sum("calamountvalue").finish();
        for (Row row :caltableRows){
            calamountValue = row.getBigDecimal("calamountvalue");
        }
        //净薪资合计
        qFilter = new QFilter("calpersonid", QCP.in,calpersonsList);
        qFilter.and(new QFilter("hsas_caltableentry.salaryitem.number",QCP.equals,netsalary));
        caltableRows = QueryServiceHelper.queryDataSet(this.getClass().getName(), "hsas_caltable", "hsas_caltableentry.calamountvalue as calamountvalue", qFilter.toArray(), null).groupBy().sum("calamountvalue").finish();
        for (Row row :caltableRows){
            netsalaryValue = row.getBigDecimal("calamountvalue");
//            this.getView().showMessage("总薪资:"+row.getString("netsalary"));
        }

        //将总薪资和净薪资反写
        qFilter = new QFilter("billno", QCP.equals,billNo);
        DynamicObject[] hrpi_perserlens = BusinessDataServiceHelper.load("hsas_approvebill", "id,nwg5_countper,nwg5_amountfield,nwg5_amountfield1", qFilter.toArray());
        for (int i=0;i<hrpi_perserlens.length;i++){
            hrpi_perserlens[i].set("nwg5_countper",totalpersonnum);
            hrpi_perserlens[i].set("nwg5_amountfield",calamountValue);
            hrpi_perserlens[i].set("nwg5_amountfield1",netsalaryValue);
        }
        SaveServiceHelper.save(hrpi_perserlens);
    }
}
